package user.ejb.eaos;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import user.ejb.eaos.UserList;
import authentication.ejb.usermanagement.User;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import model.Excursion;
import common.IPredicate;
import common.Filter;
import common.OperationCallback;
import dtos.ExcursionDTO;
import dtos.UserDTO;


@Stateless
public class UserListEAO implements UserList{
	
	@PersistenceContext
    private EntityManager em;
	
	
	
	IPredicate<User> isEmployee = new IPredicate<User>() {
		public boolean apply(User user) {
	        // binds a boolean method in User to a reference
	        return user.isEmployee();
	    }

	};
	
	public UserListEAO(){
		
	}

	@Override
	public void add(UserDTO userDTO) {
		em.persist(new User(userDTO));
		
	}

	@Override
	public Collection<UserDTO> getUserList() {
		@SuppressWarnings("unchecked")
		List<User> qres = em.createQuery("SELECT x FROM User x").getResultList();

		Collection<UserDTO> res = new ArrayList<UserDTO>(qres.size());
		for(User e : qres)
		{
			res.add(e.getUserDTO());
		}
		return res;
	}
	
	

	@Override
	public UserDTO getUser(int id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void saveUser(UserDTO u, OperationCallback oc) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void removeUser(UserDTO u, OperationCallback oc) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void updateUser(UserDTO u, OperationCallback oc) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public Collection<UserDTO> getEmployeeList() {
	
		@SuppressWarnings("unchecked")
		List<User> qres = em.createQuery("SELECT x FROM User x").getResultList();
		Collection<User> fres = Filter.filter(qres, isEmployee);
		Collection<UserDTO> res = new ArrayList<UserDTO>(fres.size());
		
		
		for(User u : fres)
		{
			res.add(u.getUserDTO());
		}
		
		return res;
		
	}
	
	
	
	


}
